package com.xingge.socketio.listener;

import java.util.Iterator;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.BroadcastOperations;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.listener.DataListener;
import com.xingge.socketio.Config;
import com.xingge.socketio.bean.ChatBean;

import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @Title:  AppDataListener.java
 * @Package com.xingge.socketio.listener
 * @Description: 消息监听器
 * @author: zhangwei
 * @date:   2022年8月6日
 * @time：	上午1:20:14
 * @version  v1.0
 * @Copyright: 2019 XingGe All rights reserved.
 */
@Slf4j
public class AppDataListener implements DataListener<ChatBean> {
	
	private SocketIOServer ioServer;
	
	public AppDataListener(SocketIOServer ioServer) {
		this.ioServer = ioServer;
	}
	
	@Override
	public void onData(SocketIOClient client, ChatBean data, AckRequest ackSender) throws Exception {
		System.err.println("This is AppDataListener onData");
		Set<String> rooms = client.getAllRooms();
		Iterator<String> room = rooms.iterator();
		while (room.hasNext()) {
			String roomName = room.next();
			if (!StringUtil.isNullOrEmpty(roomName)) {
				BroadcastOperations bo = ioServer.getRoomOperations(roomName);
				bo.sendEvent(Config.CHATEVENT, data);
				log.info("{} 的聊天室信息  :   {}", roomName, JSON.toJSON(data));
			}
		}
	}

}
